<template>
  <div class="login">
    <div class="login-form">
      <el-form :model="form" :rules="rules" ref="form" label-width="80px">
        <div class="title">发版系统</div>
        <el-form-item label="用户名:" size="mini" prop="username">
          <el-input v-model="form.username" size="mini"></el-input>
        </el-form-item>
        <el-form-item label="密码:" size="mini" prop="formPassword">
          <el-input :type="password" v-model="form.formPassword" size="mini"></el-input>
        </el-form-item>
        <el-form-item label="验证码:" size="mini" prop="captcha">
          <el-input v-model="form.captcha" size="mini"></el-input>
        </el-form-item>
        <el-form-item size="mini">
          <el-image :src="image" class="captcha" @click="acquireCaptcha"></el-image>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submit" size="mini">登录</el-button>
          <el-button type="primary" size="mini" @click="register">注册</el-button>
        </el-form-item>
      </el-form>
    </div>

    <!-- 用户注册 -->
    <user_regist :regist-dialog-visible="registDialogVisible" v-on:closeRegistDialog="closeRegistDialog"></user_regist>
  </div>
</template>

<script>
  import { login, getCaptcha } from "@/components/api/login";
  import Constants from "@/components/util/Constants";
  import Regist from "@/components/view/login/Regist";
  import md5 from 'js-md5';

  export default {
    name: "login",
    components: {
      user_regist: Regist
    },
    data() {
      return {
        form: {
          username: null,
          formPassword: null,
          password: null,
          captcha: null
        },
        image: null,
        password: Constants.PASSWORD,
        registDialogVisible: false,
        rules: {
          username: [
            { required: true, message: "请输入用户名", trigger: "blur" },
            { min: 5, max: 12, message: "长度在 5 到 12 个字符", trigger: "blur" }
          ],
          formPassword: [{ required: true, message: "请输入密码", trigger: "blur" }],
          captcha: [
            {
              required: true,
              min: 4,
              max: 4,
              message: "长度在 4 个字符",
              trigger: "blur"
            }
          ]
        }
      };
    },
    methods: {
      submit() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            this.form.password = md5(this.form.formPassword);
            this.form.formPassword = null;
            login(this.form, data => {
              if (data) {
                sessionStorage.setItem(Constants.USER_NAME, this.form.username);
                sessionStorage.setItem(Constants.TOKEN, data);
                this.username = "";
                this.password = "";
                this.$router.replace(Constants.HOME_PATH);
              }
            });
          } else {
            return false;
          }
        });
      },
      resetForm() {
        this.form.username = null;
        this.form.password = null;
        this.form.captcha = null;
      },
      acquireCaptcha() {
        getCaptcha(data => {
          if (data) {
            this.image = data;
          }
        });
      },
      register() {
        this.registDialogVisible = true;
      },
      closeRegistDialog() {
        this.registDialogVisible = false;
      }
    },
    mounted() {
      this.resetForm();
      this.acquireCaptcha();
    }
  };
</script>

<style scoped>
  .login {
    background-color: #b3c0d1;
    height: 100%;
    text-align: center;
  }

  .captcha {
    width: 250px;
    height: 30px;
  }

  .title {
    margin-bottom: 20px;
    font-size: 30px;
  }

  .login-form {
    padding: 30px 0;
    width: 450px;
    height: 300px;
    margin: 0 auto;
    position: relative;
    top: 30%;
    background-color: #e6e9f0;
  }

  .el-input {
    width: 250px;
  }
</style>